<template>
  <div class="app-container">
    <div class="filter-container">
      <el-form ref="filterForm" :model="filterForm" :inline="true" label-position="left" label-width="100px">
        <el-form-item label="账户" prop="inventorID">
          <el-input v-model="filterForm.inventorID" clearable placeholder="账户" style="width: 200px;" class="filter-item"  />
        </el-form-item>
        <el-form-item label="主力合约" prop="localSymbol">
          <el-select v-model="filterForm.localSymbol" placeholder="请选择" @keyup.enter.native="handleFilter">
            <el-option
            v-for="item in symbolOptions"
            :key="item.code"
            :label="item.fullname"
            :value="item.code">
            </el-option>
        </el-select>
        </el-form-item>
        <el-form-item label="合约" prop="symbol">
          <el-input v-model="filterForm.keyword" clearable placeholder="合约" style="width: 200px;" class="filter-item" @keyup.enter.native="handleFilter" />
        </el-form-item>
        <el-form-item label="开仓时间" prop="tradeTime">
          <el-date-picker 
          clearable
            v-model="filterForm.tradeTime"
            type="datetimerange"
            range-separator="至"
            start-placeholder="开始日期"
            end-placeholder="结束日期">
          </el-date-picker>
        </el-form-item>
      </el-form>
      <el-button @click="handleFilter()">搜索</el-button>
      <el-button @click="onPurchaseHiddenOpen()">开仓</el-button>
    </div>
    
    <el-table
      :key="tableKey"
      v-loading="listLoading"
      :data="list"
      stripe
      border
      fit
      highlight-current-row
      :height="windowHeight"
      style="width: 100%;"
      @sort-change="sortChange"
    >
        <el-table-column label="合约" prop="id" sortable="custom" align="center" width="200">
          <template slot-scope="{row}">
            <span @click="handleMarket(purchase)">{{ row.symbol }}<el-tag>{{ row.status.label }}</el-tag></span>
          </template>
        </el-table-column>
      <el-table-column label="价格" align="center" min-width="150px">
      <el-table-column label="开仓价" align="center" min-width="150px">
        <template slot-scope="{row}">
          <div>开仓价：{{ row.price }}</div>
          <div>止损价：{{ row.stopLossPrice }}</div>
          <div>止盈价：{{ row.stopProfitPrice }}</div>
        </template>
      </el-table-column>
      <el-table-column label="市场价" align="center" min-width="150px">
        <template slot-scope="{row}">
          <span>{{ row.marketPrice }}</span>
        </template>
      </el-table-column>
      </el-table-column>
      <el-table-column label="费用" min-width="150px">
        <template slot-scope="{row}">
          <span>{{ row.cost }}</span>
        </template>
      </el-table-column>
      <el-table-column label="利润" min-width="150px">
        <template slot-scope="{row}">
          <span>{{ row.profit }}</span>
        </template>
      </el-table-column>
      <el-table-column label="金额" min-width="150px">
        <template slot-scope="{row}">
          <span>{{ row.amount }}</span>
        </template>
      </el-table-column>
      <el-table-column label="数量" min-width="150px">
        <template slot-scope="{row}">
          <span>{{ row.quantity }}</span>
        </template>
      </el-table-column>
      <el-table-column label="开仓日期" min-width="180px">
        <template slot-scope="{row}">
          <span>{{ row.createdAt }}</span>
        </template>
      </el-table-column>
      <el-table-column label="过期日期" min-width="150px">
        <template slot-scope="{row}">
          <span>{{ row.expireDate }}</span>
        </template>
      </el-table-column>
      <el-table-column label="平仓价格" min-width="150px">
        <template slot-scope="{row}">
          <span>{{ row.closingPrice }}</span>
        </template>
      </el-table-column>
      <el-table-column label="识别码" min-width="150px">
        <template slot-scope="{row}">
          <span>{{ row.magic }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" width="100" fixed="right" class-name="small-padding fixed-width">
        <template slot-scope="{row}">
          <el-button size="mini" v-if="row.status.value == 5 && row.symbolType < 3" @click="onPurchaseClean(row)">
            平仓
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total>0" :total="total" :page.sync="filterForm.start" :limit.sync="filterForm.perpage" @pagination="getList" />
    <el-dialog
      title="开仓"
      :visible.sync="purchaseHiddenOpen"
      :close-on-click-modal="false"
      width="30%">
      <el-form ref="filterForm" :model="purchaseForm"  label-position="left" label-width="100px">
        <el-form-item label="合约" prop="symbol">
          <el-input v-model="purchaseForm.symbol" clearable placeholder="合约" style="width: 200px;" class="filter-item"/>
        </el-form-item>
        <el-form-item label="识别码" prop="magic">
          <el-input v-model="purchaseForm.magic" placeholder="识别码" @input="handlePurchasePriceListen" style="width: 200px;" class="filter-item" />
        </el-form-item>
        <el-form-item label="价格" prop="price">
          <el-input v-model="purchaseForm.price" placeholder="价格" @input="handlePurchasePriceListen" style="width: 200px;" class="filter-item" />
        </el-form-item>
        <el-form-item label="数量" prop="quantity">
            <el-input-number v-model="purchaseForm.quantity" @change="handlePurchaseQuantityListen" :min="1" :max="10" label="数量"></el-input-number>
        </el-form-item>
        <el-form-item label="金额" prop="amount">
          <el-input v-model="purchaseForm.amount"  readonly="readonly" placeholder="金额" />
        </el-form-item>
        <el-form-item label="手续费" prop="cost">
          <el-input v-model="purchaseForm.cost"  readonly="readonly" placeholder="手续费" />
        </el-form-item>
        <el-form-item label="总额" prop="capital">
          <el-input v-model="purchaseForm.capital" readonly="readonly" placeholder="总额" />
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="purchaseHiddenOpen = false">取 消</el-button>
        <el-button type="primary" @click="handlePurchaseSubmit()">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
//import { getFaultinessList,  removeFaultinessByID, changeFaultinessStatusByID } from '@/api/development/faultiness'
import waves from '@/directive/waves' // waves directive
import { parseTime } from '@/utils'
import Pagination from '@/components/Pagination' // secondary package based on el-pagination
import {getHost} from '@/utils/socket' // secondary package based on el-pagination
import {getPurchaseList,savePurchase} from '@/api/position/purchase'
import {getSymbolList} from '@/api/knowledge/symbol'

export default {
  name: 'financeDashboard',
  components: { Pagination },
  directives: { waves },
  filters: {
  },
  data() {
    return {
      
      socket:"",
      tableKey: 0,
      list: null,
      windowHeight:"800",
      symbolOptions:[],
      total: 0,
      listLoading: false,
      analysisData:{},
      connected:"loading",
      purchaseHiddenOpen:false,
      purchaseForm:{},
      filterForm: {
        page: 1,
        size: 10,
        localSymbol:"ag",
        inventorID:"680939",
        status:5,
        keyword: undefined
      }
    }
  },
  created() {
    this.getList()
    this.getSymbolList()
  },
  methods: {
    getList:function(){
      let _this = this;
      getPurchaseList(_this.filterForm).then((response) => {
        console.log("getList",response)
          if (response.code === 200) {
            this.list = response.data.list
            this.total = response.data.total
          } else {
            _this.$message({
              message: response.msg,
              type: 'error'
            })
          }
      })
    },
    handlePurchaseQuantityListen:function(quantity){
      console.log(quantity)
      let accountCostWeight = 0.0003
      let accountMargin = 0.2
      let symbolWeight = 20
      this.purchaseForm.cost = quantity*this.purchaseForm.price*accountCostWeight;
      this.purchaseForm.amount = quantity*this.purchaseForm.price*symbolWeight*accountMargin;
      this.purchaseForm.capital = this.purchaseForm.cost+this.purchaseForm.amount;
      this.purchaseForm.capital = Number(this.purchaseForm.capital).toFixed(2)
      this.purchaseForm.cost = Number(this.purchaseForm.cost).toFixed(2)
    },
    handlePurchasePriceListen:function(){
      let accountCostWeight = 0.0003
      let accountMargin = 0.2
      let symbolWeight = 20
      let quantity = this.purchaseForm.quantity
      this.purchaseForm.cost = quantity*this.purchaseForm.price*accountCostWeight;
      this.purchaseForm.amount = quantity*this.purchaseForm.price*symbolWeight*accountMargin;
      this.purchaseForm.capital = this.purchaseForm.cost+this.purchaseForm.amount;
      this.purchaseForm.capital = Number(this.purchaseForm.capital).toFixed(2)
      this.purchaseForm.cost = Number(this.purchaseForm.cost).toFixed(2)
    },
    getSymbolList:function(){
      let _this = this;
      console.log("期权",_this.filterForm)
      getSymbolList({
        "page":1,
        "size":100
      }).then((response) => {
        console.log("getSymbolList",response)
          if (response.code === 200) {
            this.symbolOptions = response.data.list
          } else {
            _this.$message({
              message: response.msg,
              type: 'error'
            })
          }
      })
    },
    onPurchaseHiddenOpen:function(){
      this.purchaseHiddenOpen = true
    },
    handleCreate:function(row){
      if(row == undefined){
        this.$router.push({ path: 'purchase-add'})
      }else{
        this.$router.push({ path: 'purchase-add', query: { 'parentCompanyID': row.ID }})
      }
    },
    handlePurchaseSubmit:function(){
        let _this = this;
        savePurchase(_this.purchaseForm).then((response) => {
          console.log("getList",response)
            if (response.code === 200) {
              _this.getList()
            } else {
              _this.$message({
                message: response.msg,
                type: 'error'
              })
            }
        })

    },
    handleUpdate:function(purchaseID){
        this.$router.push({ path: 'purchase-add', query: { 'purchaseID': purchaseID }})
    },
    handleFilter() {
      this.filterForm.page = 1
      this.getList()
    },
    sortChange:function(){

    }
  }
}
</script>
<style>
.filter { float: left; width: 160px; padding: 20px; min-height: 600px; }
.article { float: left; width: 800px; }
.article-item { width: 800px; height: 200px; margin-right: 25px; }
.article-item-text { float: left; width:600px; }
.article-item-icon { float: right; width:200px; height:150px; padding-left: 50px; }
.article-item-icon img { width:100%; height:100%; border-radius: 10px; }
</style>